#include "burner.h"

#define INT_INFO_STRINGS (8)

int IntInfoFree(InterfaceInfo* info)
{
	if (info->ppszInterfaceSettings) {
		for (int i = 0; i < INT_INFO_STRINGS; i++) {
			delete [] info->ppszInterfaceSettings[i];
		}
	}
	delete [] info->ppszInterfaceSettings;

	if (info->ppszModuleSettings) {
		for (int i = 0; i < INT_INFO_STRINGS; i++) {
			delete [] info->ppszModuleSettings[i];
		}
	}
	delete [] info->ppszModuleSettings;

	for (unsigned int i = 0; i < info->deviceNum; i++) {
		delete [] info->deviceName[i];
		info->deviceName[i] = NULL;
	}
	delete [] info->deviceName;
	info->deviceName = NULL;

	memset(info, 0, sizeof(InterfaceInfo));

	return 0;
}

int IntInfoInit(InterfaceInfo* info)
{
	IntInfoFree(info);

	info->ppszInterfaceSettings = new wchar_t*[INT_INFO_STRINGS + 1];
	if (info->ppszInterfaceSettings == NULL) {
		return 1;
	}
	memset(info->ppszInterfaceSettings, 0, (INT_INFO_STRINGS + 1) * sizeof(wchar_t*));

	info->ppszModuleSettings = new wchar_t*[INT_INFO_STRINGS + 1];
	if (info->ppszModuleSettings == NULL) {
		return 1;
	}
	memset(info->ppszModuleSettings, 0, (INT_INFO_STRINGS + 1) * sizeof(wchar_t*));

	return 0;
}

int IntInfoAddStringInterface(InterfaceInfo* info, wchar_t* str)
{
	int i = 0;

	for (i = 0; info->ppszInterfaceSettings[i] && i < INT_INFO_STRINGS; i++) { }

	if (i >= INT_INFO_STRINGS) {
		return 1;
	}

	info->ppszInterfaceSettings[i] = new wchar_t[MAX_PATH];
	if (info->ppszInterfaceSettings[i] == NULL) {
		return 1;
	}

	wcsncpy(info->ppszInterfaceSettings[i], str, MAX_PATH);

	return 0;
}

int IntInfoAddStringModule(InterfaceInfo* info, wchar_t* str)
{
	int i = 0;

	for (i = 0; info->ppszModuleSettings[i] && i < INT_INFO_STRINGS; i++) { }

	if (i >= INT_INFO_STRINGS) {
		return 1;
	}

	info->ppszModuleSettings[i] = new wchar_t[MAX_PATH];
	if (info->ppszModuleSettings[i] == NULL) {
		return 1;
	}

	wcsncpy(info->ppszModuleSettings[i], str, MAX_PATH);

	return 0;
}
